<?php
session_start();
if(!isset($_SESSION['uid']))
	die('{"error":2,"error_msg":"User not login."}');

if (!isset($_POST['content'])||
	!isset($_POST['receivers_id'])||
	!isset($_POST['receivers_name'])||
	!isset($_POST['share_copy']) || 
	!isset($_POST['need_follow']))
	die('{"error":5,"error_msg":"Parameter missing."}');
	
$pattern = "/^[\x7f-\xff]{12}$/";
if(!preg_match($pattern,$_POST['content'],$matchArray))
	die('{"error":6,"error_msg":"Parameter is not valid."}');

require("db_connection.php");
$sql = "INSERT INTO greetings (content,sender_id,receivers_id,sender_name,receivers_name,sender_photo) VALUES ('".$_POST['content']."','".$_SESSION['uid']."','".$_POST['receivers_id']."','".$_SESSION['name']."','".$_POST['receivers_name']."','".$_SESSION['photo']."')";

$result = mysql_query($sql);	

if($result)
{
	include_once( 'config.php' );
	include_once( 'saetv2.ex.class.php' );
	
	$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
	$friends = "@".implode(" @",explode(",",$_POST['receivers_name']));
	$shareContent =  $_POST['share_copy'].'@GreatWorks中国  '.$friends.' '.SHARE_LINK.'#gid='.mysql_insert_id();
	$weibo = $c->upload($shareContent,"images/share_icon.gif");
	if(isset($weibo['id']))
	{	
		if($_POST['need_follow'] == "yes")
		{
			$followResult = $c->follow_by_id("2588750782");
			echo '{"error":0,"error_msg":""}';
		}else{
			echo '{"error":0,"error_msg":""}';
		}
	}else{
		echo '{"error":4,"error_msg":"Sina API error."}';
	}
}else{
	echo '{"error":1,"error_msg":"Database error."}';
}

mysql_close($conn);
?>